<?php

namespace app\common\repositories\contract;

use app\common\repositories\BaseRepository;
use app\common\dao\contract\ContractBillDao;
use app\common\repositories\property\users\PropertyUsersLogRepository;
use think\facade\Db;

/**
 * @mixin ContractBillDao
 */
class ContractBillRepository extends BaseRepository
{
    const PAY_TYPE = [
        1 => '按月份支付',
        2 => '按季度支付',
        3 => '按年度支付',
        4 => '一次性支付',
        5 => '随租金支付',
    ];
    const STATUS = [
        1 => '正常',
        2 => '结束',
    ];

    public function __construct(ContractBillDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取用户设备列表
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     */
    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'cateInfo' => function ($query) {
                    $query->bind(['cate_name']);
                },
            ])
            ->order('id', 'desc')
            ->select();
        foreach ($list as $k => &$v) {
            $v['pay_type'] = self::PAY_TYPE[$v['pay_type']] ?? '';
        }
        return compact('list', 'count');
    }

    public function addInfo($propertyId, $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $res = $this->dao->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'CONTRACT.BILL.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'CONTRACT.BILL.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids], ['if_del', 1]
        ], 'id,property_id');
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'CONTRACT.BILL.DEL', $info);
        $this->dao->delete($info['id']);
    }

    public function getDetail(int $id, int $propertyId = null)
    {
        $query = $this->dao->search([], $propertyId);
        $info = $query->where('id', $id)
            ->with([
                'cateInfo' => function ($query) {
                    $query->bind(['cate_name']);
                }
            ])
            ->find();

        return $info;
    }

    public function addBill(int $contractId, int $typeId, array $data, int $type = 1)
    {
        $insertData = [];
        foreach ($data as $k => $v) {
            if ($v) {
                $insertData[] = [
                    'contract_id' => $contractId,
                    'type_id' => $typeId,
                    'is_type' => $type,
                    'image_url' => $v
                ];
            }
        }
        return $this->dao->insertAll($insertData);
    }

}
